<script setup>

import {Delete,Edit} from "@element-plus/icons-vue"
import {onMounted, ref} from "vue";
import {addCategoryReq, deleteCategoryReq, getCategoryReq, updateCategoryReq} from "@/api/fiction";
import {ElMessage, ElMessageBox} from "element-plus";

//分类数据
const categories=ref([])
onMounted(()=>{
    getCategoryList()
})

//获取小说分类
const getCategoryList = () => {
  getCategoryReq().then((respzzw)=>{
    categories.value=respzzw.data
  })
}

//弹窗显示状态
const dialogFormVisible = ref(false)
//弹窗标题
const title = ref('新增')
//
const formRef=ref()
//分类表单模型
const categoryModel = ref({
  categoryName:'',
  categoryAlias:''
})

//添加分类方法 async 异步调用方法
const addCategory = async () => {
  //主动调用表单校验
  await formRef.value.validate()
  //发送请求
  let result = await addCategoryReq(categoryModel.value);
  //获取结果告知用户
  ElMessage.success(result.message)
  //关闭弹窗
  dialogFormVisible.value=false
  //刷新表格数据
  getCategoryList()
  //刷新界面
  window.location.reload()
  //清空表单模型
  categoryModel.value={}
}

//修改小说分类
const updateCategory = async() =>{
  //主动调用表单校验
  await formRef.value.validate()
  //发送请求
  let result = await updateCategoryReq(categoryModel.value);
  ElMessage.success(result.message)
  //重新获取数据列表
  getCategoryList()
  //关闭弹窗
  dialogFormVisible.value=false
  //清空表单
  categoryModel.value={}

  window.location.reload()
}
//删除小说分类
const deleteCategory = (rowData) =>{
  console.log(rowData)
  ElMessageBox.confirm(
      '是否删除   【小说分类名称为:' + rowData.categoryName+" ID:" + rowData.id + "】  的数据",
      '温馨提醒',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //发送删除请求
        let result = await deleteCategoryReq(rowData.id);
        ElMessage({
          type: 'success',
          message: 'result.message',
        })
        //刷新表格数据
        getCategoryList()
        window.location.reload()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '删除取消',
        })
      })
}
//修改分类 回显数据
const showDialog = (rowData) => {
  //1.打开弹窗显示要修改的数据
  dialogFormVisible.value=true
  title.value='修改分类'
  categoryModel.value.categoryName=rowData.categoryName
  categoryModel.value.categoryAlias=rowData.categoryAlias
  categoryModel.value.id=rowData.id
}

const rules={
  categoryName:[
      //非空校验
      { required: true, message: '请输入分类名称', trigger: 'change' },
      //长度校验
      { min: 2, max: 10, message: '分类名称长度应为2-10', trigger: 'change' },
  ],
  categoryAlias:[
      //非空校验
      { required: true, message: '请输入分类别名', trigger: 'change' },
      //正则表达式校验
      { pattern:'^[A-Za-z]+$', message: '别名必须是英文', trigger: 'change' },
  ]
}

onMounted(()=>{
  getCategoryList()
})

</script>

<template>
  <el-card class="card-container">
    <template #header>
      <div class="card-header">
        <span>小说分类</span>
        <div>
          <el-button type="primary" @click="dialogFormVisible = true,title='新增分类'">新增小说分类</el-button>
        </div>
      </div>
    </template>
    <el-table :data="categories" style="width: 100%">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="categoryName" label="分类名称" />
      <el-table-column prop="categoryAlias" label="分类别名" />
      <el-table-column prop="createTime" label="创建时间" width="240" />
      <el-table-column prop="updateTime" label="修改时间" width="240" />
      <el-table-column fixed="right" label="操作" >
        <template #default="{row}">
          <el-button :icon="Edit" link type="warning" size="small" @click="showDialog(row)">修改</el-button>
          <el-button :icon="Delete" link type="danger" size="small" @click="deleteCategory(row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

<!--    1 弹窗的显示需要控制
        2 分类的表单内容 form 需要定义
        3 添加动作需要发生请求
-->
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
      <el-form ref="formRef" :model="categoryModel" :rules="rules">
        <el-form-item prop="categoryName" label="分类名称">
          <el-input v-model="categoryModel.categoryName" ></el-input>
        </el-form-item>
        <el-form-item prop="categoryAlias" label="分类别名">
          <el-input v-model="categoryModel.categoryAlias"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="title==='新增分类' ? addCategory():updateCategory()">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>

  </el-card>
</template>

<style scoped>
@import "@/assets/base.css";
.card-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>